//
//  GlobalParameter.m
//  PlayBall
//
//  Created by Edward Zhang on 12-5-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddGlobalParameter.h"
#import "JddCommonFunction.h"
#import "JddUserProfile.h"

@implementation JddGlobalParameter

@synthesize operationQueue;
@synthesize dictionary;

SHARED_INSTANCE_DEFINE(JddGlobalParameter);

-(void)load
{
    self.operationQueue = [[NSOperationQueue alloc] init];
    [self.operationQueue setMaxConcurrentOperationCount:8];
        
    NSString *globalParameterFile = [NSString stringWithFormat:@"%@/globalParameter.dat", [JddCommonFunction thisDocumentPath]];
    if ([[NSFileManager defaultManager] fileExistsAtPath:globalParameterFile isDirectory:NO]) {
        self.dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:globalParameterFile];
        [JddUserProfile sharedInstance].name = [self.dictionary objectForKey:GLOBAL_PARAMETER_USER];
        [JddUserProfile sharedInstance].password = [self.dictionary objectForKey:GLOBAL_PARAMETER_PASSWORD];
        [JddUserProfile sharedInstance].autoLogon = [[self.dictionary objectForKey:GLOBAL_PARAMETER_AUTO_LOGON] boolValue];
        [JddUserProfile sharedInstance].userId = [self.dictionary objectForKey:GLOBAL_PARAMETER_USER_ID];
    }
    else {
        self.dictionary = [NSMutableDictionary dictionary];
    }
}

-(void)save
{
    if ([[self.dictionary allKeys] count]) {
        [self.dictionary writeToFile:[NSString stringWithFormat:@"%@/globalParameter.dat", [JddCommonFunction thisDocumentPath]] atomically:YES];
    }
}

@end
